﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Interactivity;

namespace BaseLibrary
{
    public class CheckForUpdateBehavior : TargetedTriggerAction<FrameworkElement>
    {
        UIElement _element;

        protected override void OnAttached()
        {
            // Works only out-of-browser
            if (Application.Current.IsRunningOutOfBrowser)
            {
                _element = this.AssociatedObject as UIElement;
            }
            base.OnAttached();
        }

        protected override void Invoke(object o)
        {
            if (_element != null)
            {
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    Application.Current.CheckAndDownloadUpdateCompleted += (s, e) =>
                    {
                        if (e.UpdateAvailable)
                        {
                            if (ShowUpdateIsAvailableMessage)
                            {
                                MessageBox.Show(UpdateIsAvailableMessageText);
                            }
                            if (CloseAfterUpdateMessage)
                            {
                                Application.Current.MainWindow.Close();
                            }
                        }
                        else
                        {
                            if (ShowMessageWhenNoUpdateIsAvailable)
                            {
                                MessageBox.Show(NoUpdateAvailableMessage);
                            }
                        }
                    };
                    Application.Current.CheckAndDownloadUpdateAsync();
                }
            }
        }

        #region Properties

        public bool ShowUpdateIsAvailableMessage
        {
            get { return (bool)GetValue(ShowUpdateIsAvailableMessageProperty); }
            set { SetValue(ShowUpdateIsAvailableMessageProperty, value); }
        }

        public static readonly DependencyProperty ShowUpdateIsAvailableMessageProperty =
            DependencyProperty.Register(
                "ShowUpdateIsAvailableMessage", 
                typeof(bool), 
                typeof(CheckForUpdateBehavior), 
                new PropertyMetadata(true));

        public string UpdateIsAvailableMessageText
        {
            get { return (string)GetValue(UpdateIsAvailableMessageTextProperty); }
            set { SetValue(UpdateIsAvailableMessageTextProperty, value); }
        }

        public static readonly DependencyProperty UpdateIsAvailableMessageTextProperty =
            DependencyProperty.Register(
                "UpdateIsAvailableMessageText",
                typeof(string),
                typeof(CheckForUpdateBehavior),
                new PropertyMetadata("Update is available. Please restart your application now!"));
        
        //private bool Network;

        public bool CloseAfterUpdateMessage
        {
            get { return (bool)GetValue(CloseAfterUpdateMessageProperty); }
            set { SetValue(CloseAfterUpdateMessageProperty, value); }
        }

        public static readonly DependencyProperty CloseAfterUpdateMessageProperty =
            DependencyProperty.Register(
                "CloseAfterUpdateMessage", 
                typeof(bool), 
                typeof(CheckForUpdateBehavior), 
                new PropertyMetadata(true));

        public bool ShowMessageWhenNoUpdateIsAvailable
        {
            get { return (bool)GetValue(ShowMessageWhenNoUpdateIsAvailableProperty); }
            set { SetValue(ShowMessageWhenNoUpdateIsAvailableProperty, value); }
        }

        public static readonly DependencyProperty ShowMessageWhenNoUpdateIsAvailableProperty =
            DependencyProperty.Register(
                "ShowMessageWhenNoUpdateIsAvailable", 
                typeof(bool), 
                typeof(CheckForUpdateBehavior), 
                new PropertyMetadata(false));

        public string NoUpdateAvailableMessage
        {
            get { return (string)GetValue(NoUpdateAvailableMessageProperty); }
            set { SetValue(NoUpdateAvailableMessageProperty, value); }
        }

        public static readonly DependencyProperty NoUpdateAvailableMessageProperty =
            DependencyProperty.Register(
                "NoUpdateAvailableMessage", 
                typeof(string), 
                typeof(CheckForUpdateBehavior), 
                new PropertyMetadata("No update available."));

        #endregion

    }
}
